package com.coffeeloversupportlibrary.coffeelover.contentshow;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.lifecycle.Observer;

import com.coffeeloversupportlibrary.R;
import com.coffeeloversupportlibrary.core.base.BaseActivity;
import com.coffeeloversupportlibrary.databinding.ActivityArticleDetailsBinding;
import com.zzhoujay.richtext.ImageHolder;
import com.zzhoujay.richtext.RichText;

/**
 * @author puyv
 * @createTime 2021/11/22 20:12
 * @projectName CoffeeLoverSupportLibrary
 * @className Details.java
 * @description TODO
 */
public class Details extends BaseActivity<ArticleDetailsViewModel, ActivityArticleDetailsBinding> {
    private String title;
    @Override
    public void registerUIChangeCallBack() {
           getViewModel().getArticleContent().observe(this, new Observer<String>() {
               @Override
               public void onChanged(String s) {
                   init(s);
               }
           });
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_article_details;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        RichText.initCacheDir(this);
        Intent intent=getIntent();
        String articleId=intent.getStringExtra("articleId");
        title=intent.getStringExtra("title");
        getViewModel().loadArticle(articleId);
    }

    private void init(String content){
       getBinding().articleTitle.setText(title);
        RichText.from(content).bind(this)
                .size(ImageHolder.MATCH_PARENT, ImageHolder.WRAP_CONTENT)
                .into(getBinding().textView);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        RichText.clear(this);
    }
}
